package cn.minis.beans.factory.xml;

import cn.minis.beans.BeanFactory;
import cn.minis.beans.factory.AbstractBeanFactory;
import cn.minis.beans.factory.config.ConstructorArgumentValue;
import cn.minis.beans.factory.config.ConstructorArgumentValues;
import cn.minis.beans.factory.config.BeanDefinition;
import cn.minis.beans.factory.config.PropertyValue;
import cn.minis.beans.factory.config.PropertyValues;
import cn.minis.beans.Resource;
import cn.minis.beans.factory.support.SimpleBeanFactory;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;

public class XmlBeanDefinitionReader {
   /* BeanFactory beanFactory;
    public XmlBeanDefinitionReader(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }
    
    */
  /**
   * loadBeanDefinitions 方法会把解析的 XML 内容转换成 BeanDefinition，并加载到 BeanFactory 中
   *
   * @param resource
   *//*
    public void loadBeanDefinitions(Resource resource) {
        while (resource.hasNext()) {
            Element element = (Element) resource.next();
            String beanID = element.attributeValue("id");
            String beanClassName = element.attributeValue("class");
            BeanDefinition beanDefinition = new BeanDefinition(beanID, beanClassName);
            //this.beanFactory.registerBeanDefinition(beanDefinition);
        }
    }*/
  
  
  AbstractBeanFactory beanFactory;
  
  public XmlBeanDefinitionReader(AbstractBeanFactory beanFactory) {
    this.beanFactory = beanFactory;
  }
  
  public void loadBeanDefinitions(Resource resource) {
    while (resource.hasNext()) {
      Element element = (Element) resource.next();
      String beanId = element.attributeValue("id");
      String beanClassName = element.attributeValue("class");
      BeanDefinition beanDefinition = new BeanDefinition(beanId, beanClassName);
  
      //处理属性
      List<Element> propertyElements = element.elements("property");
      PropertyValues PVS = new PropertyValues();
      List<String> refs = new ArrayList<>();
      
      for (Element e : propertyElements) {
        String pType = e.attributeValue("type");
        String pName = e.attributeValue("name");
        String pValue = e.attributeValue("value");
        String pRef = e.attributeValue("ref");
  
        // 实际应该注入哪一个
        String pV = "";
        boolean isRef = false;
        if (pValue != null && !"".equals(pValue)) {
          isRef = false;
          pV = pValue;
        } else if (pRef != null && !"".equals(pRef)) {
          isRef = true;
          pV = pRef;
          refs.add(pRef);
        }
        
        PVS.addPropertyValue(new PropertyValue(pType, pName, pV, isRef));
      }
      // 依赖注入
      String[] refArray = refs.toArray(new String[0]);
      beanDefinition.setDependsOn(refArray);
      beanDefinition.setPropertyValues(PVS);
  
      //处理构造器参数
      List<Element> constructorElements = element.elements("constructor-arg");
      
      ConstructorArgumentValues constructArgValues = new ConstructorArgumentValues();
      for (Element e : constructorElements) {
        String aType = e.attributeValue("type");
        String aName = e.attributeValue("name");
        String aValue = e.attributeValue("value");
        constructArgValues.addArgumentValue(0, new ConstructorArgumentValue(aType, aName, aValue));
      }
      beanDefinition.setConstructorArgumentValues(constructArgValues);
   
      this.beanFactory.registerBeanDefinition(beanId, beanDefinition);
    }
  }
}